package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* PersonalizedDressingUpView
* */
class PersonalizedDressingUpView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            style = Paint.Style.STROKE
            pathEffect = CornerPathEffect(context.resources.getDimension(R.dimen.dp_5))
            color = context.resources.getColor(R.color.color_A98DE8)
        }
    }

    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

            mPaint0.strokeWidth = (cx / 15).coerceAtLeast(minStrokeWidth)
            mPath.apply {
//                arcTo(
//                    -cx * 4 / 7,
//                    -cy * 11 / 7,
//                    cx * 4 / 7,
//                    -cy * 3 / 7,
//                    30F,
//                    120F,
//                    true
//                )
//                lineTo(-cx * 6 / 7, -cy * 3 / 7)
//                lineTo(-cx * 4 / 7, -cy * 1 / 7)
//                lineTo(-cx * 11 / 28, -cy * 3 / 14)
//                lineTo(-cx * 11 / 28, cy * 5 / 7)
//                lineTo(cx * 11 / 28, cy * 5 / 7)
//                lineTo(cx * 11 / 28, -cy * 3 / 14)
//                lineTo(cx * 4 / 7, -cy * 1 / 7)
//                lineTo(cx * 6 / 7, -cy * 3 / 7)
//                close()
                arcTo(
                    -cx * 4 / 7,
                    -cy * 25 / 14,
                    cx * 4 / 7,
                    -cy * 9 / 14,
                    30F,
                    120F,
                    true
                )
                lineTo(-cx * 46 / 49, -cy * 3 / 7)
                lineTo(-cx * 5 / 7, -cy * 1 / 7)
                lineTo(-cx * 4 / 7, -cy * 2 / 7)
                lineTo(-cx * 4 / 7, cy * 46 / 49)
                lineTo(cx * 4 / 7, cy * 46 / 49)
                lineTo(cx * 4 / 7, -cy * 2 / 7)
                lineTo(cx * 5 / 7, -cy * 1 / 7)
                lineTo(cx * 46 / 49, -cy * 3 / 7)
                close()
            }
            drawPath(mPath, mPaint0)

            restore()
        }
    }
}